package com.faithbj.device.web.controller;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.faithbj.device.model.configuration.Pager;
import com.faithbj.device.model.entity.Article;
import com.faithbj.device.model.entity.Members;
import com.faithbj.device.model.entity.Roles;
import com.faithbj.device.service.ArticleService;
import com.faithbj.device.service.MemberService;
import com.faithbj.device.support.filedownload.FileDownloadHandler;

@Controller
public class IndexController implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3191027650216346092L;
	
	@Resource
	private ArticleService articleService;
    @Resource
    private FileDownloadHandler fileDownloadHandler;
    
	@Resource
	private MemberService memberService;
    
	@RequestMapping("index")
	public String index(ModelMap map) {
		
		List<Article> newest=articleService.getArticleList("4028824038224e29013822522c380000", 0, 10);
		List<Article> announce=articleService.getArticleList("402881e4382b3d6a01382b6028740004", 0, 10);
		
		map.put("newest", newest);
		map.put("announce", announce);
		
		Article article = articleService.load("ff8080813b3f9e98013b40c9ae4d0001");
		map.put("article", article);
		return "web/index";
	}
	
	@RequestMapping("singlecat/{id}")
	public String index(@PathVariable(value = "id") String id,Pager pager,ModelMap map) {
		
		List<Article> items=articleService.getArticleList(id, pager.getFirstResult(), pager.getPageSize());
		Integer totalCount=articleService.getArticleListCount(id);
		pager.setResult(items);
		pager.setTotalCount(totalCount);
		map.put("pager", pager);
		
		List<Article> newest=articleService.getArticleList("4028824038224e29013822522c380000", 0, 10);
		List<Article> announce=articleService.getArticleList("402881e4382b3d6a01382b6028740004", 0, 10);
		
		map.put("newest", newest);
		map.put("announce", announce);
		
		Article article = articleService.load("402881e4382ba02801382bac8afa0002");
		map.put("article", article);
		return "web/article";
	}
	
	@RequestMapping(value = "/showimage")
	public ResponseEntity<byte[]> showimage(@RequestParam(value = "id", required = false) String id)
	{
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.IMAGE_JPEG);
	
        return new ResponseEntity<byte[]>(fileDownloadHandler.getFileBinary(id), headers, HttpStatus.OK);
	}
	
	// 保存
	@RequestMapping(value="/register",method=RequestMethod.POST)
	@ResponseBody
	public ModelMap register(Members member,HttpServletRequest request) {
		ModelMap map=new ModelMap();
        Members tm=memberService.getMemberByUsername(member.getUsername());
        if(tm!=null)
        {
            map.put("failture", true);
            return map;
        }

		Set<Roles> roles=new HashSet<Roles>();
		Roles role=new Roles();
		role.setId("402880e4378db0e901378db0f3650002");
		role.setName("user");
		role.setValue("ROLE_USER");

		Set<Members> members=new HashSet<Members>();
		members.add(member);
		role.setMembers(members);

		roles.add(role);

		member.setUsername(member.getUsername().trim());
		member.setAccountNonLocked(true);
		member.setLoginFailureCount(0);
		member.setLockedDate(null);
		member.setLoginDate(new Date());
		member.setRegisterIp(request.getRemoteAddr());
		member.setLoginIp(request.getRemoteAddr());

		member.setRoles(roles);

		memberService.save(member);
		
        map.put("success", true);
        map.put("status", "success");
        return map;
	}

}
